#-*-coding:utf-8-*-
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.utils.html import mark_safe
from django.core.paginator import *
from django.views.generic import View
import requests as req
import json


from utils.zabbix import login_zabbix ,get_zabbix_servers, get_zabbix_items
from utils.constant import ACTIVED
from utils.view import get_params


# Create your views here.


class ZabbixView(View):
    def get(self, request):
        if request.user.is_authenticated():
            token = login_zabbix()
            servers = get_zabbix_servers(token)
            search, start, end, entry, num = get_params(request)
            context = {'zabbix': ACTIVED,
                       'servers': servers,
                       'entry': entry,
                       'table_class': mark_safe('table table-striped'),
                       'table_title': u'Zabbix监控'}
            if not search:
                paginator = Paginator(servers, entry)
                servers = paginator.page(num)
                context['objects'] = servers
            else:
                temp = list()
                for server in servers:
                    if search in server['host']:
                        temp.append(server)
                    if search in server['hostid']:
                        temp.append(server)
                    if search == server['interfaces'][0]['ip']:
                        temp.append(server)
                paginator = Paginator(temp, entry)
                servers = paginator.page(num)
                context['objects'] = servers
                context['search'] = 'top-search=' + search
                context['last_search'] = search
            return render(request, 'Zabbix/servers.html', context=context)


class ItemView(View):
    def get(self, request, hostid, host):
        if request.user.is_authenticated():
            search, start, end, entry, num = get_params(request)
            print entry
            token = login_zabbix()
            items = get_zabbix_items(hostid, token)
            context = {'zabbix': ACTIVED,
                       'table_title': host,
                       'hostid': hostid,
                       'entry': entry,
                       'table_class': mark_safe('table table-bordered')}
            if not search:
                paginator = Paginator(items, entry)
                items = paginator.page(num)
                context['objects'] = items
            else:
                temp = list()
                for item in items:
                    if search in item['name']:
                        temp.append(item)
                paginator = Paginator(temp, entry)
                temp = paginator.page(num)
                context['objects'] = temp
                context['search'] = 'top-search=' + search
                context['last_search'] = search
            return render(request, 'Zabbix/items.html', context=context)
        else:
            return HttpResponse(status=403)


def get_server_items(request):
    if request.user.is_authenticated():
        hostid = request.POST.get('hostid')
        token = login_zabbix()
        items = get_zabbix_items(hostid, token)
        items = json.dumps(items)
        return JsonResponse({"items": items})
    else:
        return HttpResponse(status=403)
